package ee.az.test.gui.event;

import java.awt.BorderLayout;
import java.awt.TextArea;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class KeyboardEventViewer extends JFrame {

	private static final long serialVersionUID = 1L;
	private TextArea console;

	public KeyboardEventViewer() {
		setLayout(new BorderLayout());
		console = new TextArea();
		console.setEditable(false);
		add(console, BorderLayout.CENTER);
		
		console.addKeyListener(new KeyListener() {
			public void keyTyped(KeyEvent e) {
				say(e.toString());
				e.consume();
			}
			public void keyReleased(KeyEvent e) {
				say(e.toString());
				e.consume();
			}
			public void keyPressed(KeyEvent e) {
				say(e.toString());
				e.consume();
			}
		});
	}

	private void say(String msg) {
		console.setText(msg + "\n" + console.getText());
	}
	
	public static void main(String[] args) {
		KeyboardEventViewer window = new KeyboardEventViewer();
        try {
            window.setSize(400, 300);
            window.setTitle( "Keyboard viewer" );
            window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
            window.setVisible( true );
        } catch ( Exception e ) {
            JOptionPane.showMessageDialog( window, "Unable to start program: " + e.getMessage(), "Program error.", JOptionPane.ERROR_MESSAGE );
            System.exit( 1 ); // error exit
        }
	}
}
